using System;
using System.Collections;
using System.Windows.Forms;
using Evaluant.Uss;
using Evaluant.Uss.PersistenceManager;
using Model;


namespace GeneratedORMapping
{
	public class Program
	{	
		[STAThread]
		static void Main()
		{	
			// Create the PersistenceManager with PersistenceManagerFactory after registring meta data about the model
			ObjectServices os = new ObjectServices("engines.config");
			ObjectContext oc = pmf.CreateObjectContext();

			//Initializing the database
			pm.InitializeRepository();

			//Creating Company and Employees objects
			Employee e1 = new Employee();
			e1.FirstName = "John";
			e1.Name = "Smith";
			e1.Salary = 2000;

			Employee e2 = new Employee();
			e2.FirstName = "Roland";
			e2.Name = "Mendel";
			e2.Salary = 3000;

			Company c = new Company();
			c.Name = "Janete Limeira";
			c.Chairman = "Alfreds";
			c.Employees.Add(e1);
			c.Employees.Add(e2);

			//Serializing the Company and all Employees
			oc.BeginTransaction();
			oc.Serialize(c);
			oc.CommitTransaction();

			//Loading specific Company from criteria
			IList companies = pm.Load(new Query(typeof(Company), "[Name='Janete Limeira']"));

			//Loading scalar value (total number of employees)
			int nbEmployees = (int)oc.LoadScalar("count(Model:Company[Name='Janete Limeira'].Employees)");

			//Write the loaded Company (Parent) and its Employees (Children)
			foreach(Company company in companies)
			{
				Console.WriteLine(String.Format("{0} : {1} employee(s)", company.Name, nbEmployees));
				foreach(Employee e in company.Employees)
				{
					Console.WriteLine("\t" + e.Name);
				}
			}

			Console.WriteLine("End of program...");
			Console.ReadLine();
		}

	}
}
